package com.star.warmwinter.widget;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.star.tool.util.ToastUtils;
import com.star.warmwinter.App;
import com.star.warmwinter.R;


/**
 * Author: blackbai11
 * Date: 2018/5/31
 * Description: 自定义app主题风格Toast
 */
public class ThemeToast {

    private static final Handler HANDLER = new Handler(Looper.getMainLooper());

    //字符串,短时
    public static void showShort(@NonNull final CharSequence text) {
        show(text, Toast.LENGTH_SHORT);
    }

    //字符资源,短时
    public static void showShort(@StringRes final int resId) {
        show(resId, Toast.LENGTH_SHORT);
    }

    //字符资源,format参数,短时
    public static void showShort(@StringRes final int resId, final Object... args) {
        show(resId, Toast.LENGTH_SHORT, args);
    }

    //format规则,参数,短时
    public static void showShort(final String format, final Object... args) {
        show(format, Toast.LENGTH_SHORT, args);
    }

    //字符串,长时
    public static void showLong(@NonNull final CharSequence text) {
        show(text, Toast.LENGTH_LONG);
    }

    //字符资源,长时
    public static void showLong(@StringRes final int resId) {
        show(resId, Toast.LENGTH_LONG);
    }

    //字符资源,format参数,长时
    public static void showLong(@StringRes final int resId, final Object... args) {
        show(resId, Toast.LENGTH_LONG, args);
    }

    //format规则,参数,长时
    public static void showLong(final String format, final Object... args) {
        show(format, Toast.LENGTH_LONG, args);
    }

    private static void show(@StringRes final int resId, final int duration) {
        show(App.getInstance().getResources().getString(resId), duration);
    }

    private static void show(@StringRes final int resId, final int duration, final Object... args) {
        show(String.format(App.getInstance().getResources().getString(resId), args), duration);
    }

    private static void show(final String format, final int duration, final Object... args) {
        show(String.format(format, args), duration);
    }

    /**以上方法都是为了传多种形式的字符串,其他自定义Toast可以直接拷贝
     * *********************************************************************************************
     * 核心需要自定义的地方
     * findViewById 拿到具体控件具体控制
     */
    public static void show(final CharSequence text, final int duration) {
        HANDLER.post(new Runnable() {
            @Override
            public void run() {
                cancel();
                ToastUtils.setGravity(Gravity.FILL, 0, 0);
                View view = LayoutInflater.from(App.getInstance()).inflate(R.layout.layout_theme_toast, null);
                TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);
                tv_toast.setText(text);
                if (duration == Toast.LENGTH_SHORT) {
                    ToastUtils.showCustomShort(view);
                } else {
                    ToastUtils.showCustomLong(view);
                }
            }
        });
    }

    private static void cancel() {
        ToastUtils.cancel();
    }
}
